function quickViewMap(varargin)
% quickViewMap
%
% Use this to look at maps and related data stored in data M files
%
% Notes:
% - map data sent to 'currentMap' variable in the workspace
% - NB: assumes m file names are not overloaded; i.e., 1 copy/path

% TODO:
% - soma marker
% - allow to be called with user-defined map type as input argument

% problem files -- gs2213, gs2215

% editing:
% gs nov-dec 2007
% ---------------------------------------------------
if nargin == 0
    mode = 'init';
else
    mode = varargin{1};
end

% preferred default directory
defaultDirectory = 'C:\_Cartography\matlab\dataMfiles\Gordon';

switch mode
    case 'init'
        try
            cd(defaultDirectory);
        catch
            cd(matlabroot);
        end
        [filename, pathname] = uigetfile('*.m', 'Select a data m-file');
        % if filename == 0 | pathname == 0
        if isequal(filename,0)|isequal(pathname,0)
            return
        end
        fullfilename = fullfile(pathname, filename);
        [pathstr, mapname, ext, versn] = fileparts(fullfilename);
        data.completename = fullfile(pathstr, [mapname, ext, versn]);

        hfig = figure(2000);
        clf;
%         set(hfig, 'UserData', data.completename);
        set(hfig, 'Color', 'w', 'Name', 'quickViewMap', ...
            'NumberTitle', 'off', 'Position', [20 385 1110 340]);
        colormap(flipud(colormap(jet2(256))));

    case 'select new file'
        hfig = figure(2000);
        data = get(gcf, 'UserData');
        clf;
        saveCurrentDirectory = pwd;
%         [pathstr, mapname, ext, versn] = fileparts(data.completename);
%         cd(pathstr);
        [filename, pathname] = uigetfile('*.m', 'Select a data m-file');
        if isequal(filename,0)|isequal(pathname,0)
            return
        end
        fullfilename = fullfile(pathname, filename);
        [pathstr, mapname, ext, versn] = fileparts(fullfilename);
        data.completename = fullfile(pathstr, [mapname, ext, versn]);
%         cd(saveCurrentDirectory)

%         % figure
%         hfig = figure(2000);
%         data = get(gcf, 'UserData');
%         clf;
%         set(hfig, 'Color', 'w', 'Name', 'quickViewMap', ...
%             'NumberTitle', 'off', 'Position', [20 300 1200 425]);
%         set(hfig, 'UserData', data.completename);

    case 'scroll backwards'
        hfig = figure(2000);
        data = get(gcf, 'UserData');
        clf;
        set(hfig, 'Color', 'w', 'Name', 'quickViewMap', 'NumberTitle', 'off');
        [pathstr, mapname, ext, versn] = fileparts(data.completename);
        
        % something wrong here: ========
%         files = dir(fullfile(pathstr, ['*' ext]));
%         size(files)
        
        files = dir('*.m');
%         size(files)
        %===============================
        c = struct2cell(files);
        list = char(c{1,:});
        k = strmatch(mapname, list);
        if k > 1
            newfilename = list(k-1,:); % file name of the preceding (k-1) file
            data.completename = fullfile(pathstr, newfilename);
%             [pathstr, mapname, ext, versn] = fileparts(data.completename);
        end

    case 'scroll forwards'
        hfig = figure(2000);
        data = get(gcf, 'UserData');
        clf;
        set(hfig, 'Color', 'w', 'Name', 'quickViewMap', 'NumberTitle', 'off');
        [pathstr, mapname, ext, versn] = fileparts(data.completename);
        files = dir(fullfile(pathstr, ['*' ext]));
files
% fprintf(1, '%s in %s\n', num2str(length(fullfile(pathstr, ['*' ext]))), fullfile(pathstr, ['*' ext]));
fprintf(1, '%s in %s\n', num2str(length(files)), fullfile(pathstr, ['*' ext]));
%         files = dir('*.m');
        c = struct2cell(files);
        list = char(c{1,:});
        k = strmatch(mapname, list);
        if k < size(list, 1)
            newfilename = list(k+1,:); % file name of the following (k+1) file
            data.completename = fullfile(pathstr, newfilename);
%             [pathstr, mapname, ext, versn] = fileparts(data.completename);
        end

%     case 'header to command line'
%         hfig = figure(2000);
%         data = get(gcf, 'UserData');
%         eval('data');
%         return
end
set(hfig, 'UserData', data);

% ======== uicontrols============================
hscrollerbackwards = uicontrol('Style', 'pushbutton', 'String', '<<', ...
    'Units', 'normalized', ...
    'Position', [.92 .93 .04 .07], 'Callback', 'quickViewMap(''scroll backwards'')');

hscrollerforwards = uicontrol('Style', 'pushbutton', 'String', '>>', ...
    'Units', 'normalized', ...
    'Position', [.96 .93 .04 .07], 'Callback', 'quickViewMap(''scroll forwards'')');

hselectnewfile = uicontrol('Style', 'pushbutton', 'String', 'New', ...
    'Units', 'normalized', ...
    'Position', [.92 .86 .08 .07], 'Callback', 'quickViewMap(''select new file'')');

% hheaderprint = uicontrol('Style', 'pushbutton', 'String', 'Header', ...
%     'Units', 'normalized', ...
%     'Position', [.92 .79 .08 .07], 'Callback', 'quickViewMap(''header to command line'')');
% ================================================
% return

% open the file, extract the data
currentMap = eval(mapname);

% put the data in the workspace
assignin('base', 'currentMap', currentMap);

% % store in figure's userData
% data.currentMap = currentMap;
% set(hfig, 'UserData', data);

% conditionally dump the structure variable to the command line
commandLineFlag = 0;
if commandLineFlag
    currentMap
end

% set up subplot array
subRows = 2; 
subCols = 4;
plotnum = 0;
plotToSkip = 5; % leaves room for info, plotted to first axes

% axes for info display
plotnum = plotnum + 1;
hsub = subplot(subRows, subCols, plotnum);
set(hsub, 'Visible', 'off'); % ', 'XTick', [], 'YTick', []);
% title(currentMap.experiment);
xTxt = .05; 
yTxt = 1.2;
dyTxt = .1;
nTxt = 0;
nTxt = nTxt + 1; 

if isfield(currentMap, 'experiment')
    str = ['expt: ' num2str(currentMap.experiment)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'exptCondition')
    str = ['expt condition: ' num2str(currentMap.exptCondition)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'age')
    str = ['age: ' num2str(currentMap.age)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'cellType')
    str = ['cell type: ' num2str(currentMap.cellType)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'z')
    str = ['depth (z, in um): ' num2str(currentMap.z)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'region')
    str = ['region: ' currentMap.region];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'yfrac')
    if findstr(currentMap.yfrac, '.') == 1 % add leading zero for display
            currentMap.yfrac = ['0' currentMap.yfrac];
    end
    str = ['yfrac: ' num2str(currentMap.yfrac)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'layer')
    str = ['layer: ' currentMap.layer];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'type')
    str = ['map type: ' currentMap.type];
    yTxt = yTxt - dyTxt;
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'mapNumbers')
    str = ['map #s: ' num2str(currentMap.mapNumbers)];
    yTxt = yTxt - dyTxt; ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'laserPower')
    str = ['laser power: ' num2str(currentMap.laserPower)];
    yTxt = yTxt - dyTxt; ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'mapSpacing')
    xy = currentMap.mapSpacing;
    if numel(xy) == 1
        xy = [xy xy];
        disp([currentMap.experiment ...
            ': only 1 map spacing parameter found; assuming X and Y spacing is equal.']);
    end
    str = ['map spacing: ' num2str(currentMap.mapSpacing)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
else
    disp([currentMap.experiment ': No map spacing parameter found; defaulting to 100 um square.']);
    xy = [100 100];
    str = ['map spacing: ' num2str(xy)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'totalNumberOfSpikes')
    str = ['# of spikes: ' num2str(currentMap.totalNumberOfSpikes)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'spikesPerSite')
    str = ['spikes per site: ' num2str(currentMap.spikesPerSite)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'meanWeightedDistanceFromSoma')
    str = ['mean distance: ' num2str(currentMap.meanWeightedDistanceFromSoma)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

if isfield(currentMap, 'notes')
    str = ['notes: ' num2str(currentMap.notes)];
    yTxt = yTxt - dyTxt; 
    ht = text(xTxt, yTxt, str);
end

% =========== MAPS ================
if isfield(currentMap, 'xyNew')
    xysoma = -currentMap.xyNew;
else
    xysoma = currentMap.xy;
end
    
% genMean map
if isfield(currentMap, 'genMean')
    plotnum = plotnum + 1;
    if plotnum == plotToSkip; plotnum = plotnum + 1; end
    hsub = subplot(subRows, subCols, plotnum);
    m = currentMap.genMean;
    himg = imagesc(nan2inf(currentMap.genMean));
    mapSize = size(currentMap.genMean);
    xrange = ([0 (mapSize(2) - 1)] - (mapSize(2) - 1)/2) * xy(1);
    yrange = ([0 (mapSize(1) - 1)] - (mapSize(1) - 1)/2) * xy(2);
    set(himg, 'XData', xrange, 'YData', yrange);
    set(hsub, 'XLim', [xrange(1)-xy(1)/2 xrange(2)+xy(1)/2], ...
        'YLim', [yrange(1)-xy(2)/2 yrange(2)+xy(2)/2]);  
    daspect([xy 1]);
    title('genMean')
    hold on
    hsoma = plot(xysoma(1), xysoma(2), 'wo');
    colorbar;
else
%     disp([currentMap.experiment ': No genMean map found.']);
end

% edMean map
if isfield(currentMap, 'edMean')
    plotnum = plotnum + 1;
    if plotnum == plotToSkip; plotnum = plotnum + 1; end
    hsub = subplot(subRows, subCols, plotnum);
    himg = imagesc(nan2inf(currentMap.edMean));
    mapSize = size(currentMap.edMean);
    xrange = ([0 (mapSize(2) - 1)] - (mapSize(2) - 1)/2) * xy(1);
    yrange = ([0 (mapSize(1) - 1)] - (mapSize(1) - 1)/2) * xy(2);
    set(himg, 'XData', xrange, 'YData', yrange);
    set(hsub, 'XLim', [xrange(1)-xy(1)/2 xrange(2)+xy(1)/2], ...
        'YLim', [yrange(1)-xy(2)/2 yrange(2)+xy(2)/2]);  
    daspect([xy 1]);
    title('edMean')
    hold on
    hsoma = plot(xysoma(1), xysoma(2), 'wo');
    colorbar;
else
%     disp([currentMap.experiment ': No edMean map found.']);
end

% mapAvgMean4th map
if isfield(currentMap, 'mapAvgMean4th')
    plotnum = plotnum + 1;
    if plotnum == plotToSkip; plotnum = plotnum + 1; end
    hsub = subplot(subRows, subCols, plotnum);
    himg = imagesc(currentMap.mapAvgMean4th);
    mapSize = size(currentMap.mapAvgMean4th);
    xrange = ([0 (mapSize(2) - 1)] - (mapSize(2) - 1)/2) * xy(1);
    yrange = ([0 (mapSize(1) - 1)] - (mapSize(1) - 1)/2) * xy(2);
    set(himg, 'XData', xrange, 'YData', yrange);
    set(hsub, 'XLim', [xrange(1)-xy(1)/2 xrange(2)+xy(1)/2], ...
        'YLim', [yrange(1)-xy(2)/2 yrange(2)+xy(2)/2]);  
    daspect([xy 1]);
    title('mapAvgMean4th')
    hold on
    hsoma = plot(xysoma(1), xysoma(2), 'wo');
    colorbar;
else
%     disp([currentMap.experiment ': No mapAvgMean4th map found.']);
end

% inhib map
if isfield(currentMap, 'inhib')
    plotnum = plotnum + 1;
    if plotnum == plotToSkip; plotnum = plotnum + 1; end
    hsub = subplot(subRows, subCols, plotnum);
    himg = imagesc(currentMap.inhib);
    mapSize = size(currentMap.inhib);
    xrange = ([0 (mapSize(2) - 1)] - (mapSize(2) - 1)/2) * xy(1);
    yrange = ([0 (mapSize(1) - 1)] - (mapSize(1) - 1)/2) * xy(2);
    set(himg, 'XData', xrange, 'YData', yrange);
    set(hsub, 'XLim', [xrange(1)-xy(1)/2 xrange(2)+xy(1)/2], ...
        'YLim', [yrange(1)-xy(2)/2 yrange(2)+xy(2)/2]);  
    daspect([xy 1]);
    title('inhib')
    hold on
    hsoma = plot(xysoma(1), xysoma(2), 'wo');
    colorbar;
else
%     disp([currentMap.experiment ': No mapAvgMean4th map found.']);
end

% apNumber map
if isfield(currentMap, 'apNumber')
    plotnum = plotnum + 1;
    if plotnum == plotToSkip; plotnum = plotnum + 1; end
    hsub = subplot(subRows, subCols, plotnum);
    himg = imagesc(currentMap.apNumber);
    mapSize = size(currentMap.apNumber);
    xrange = ([0 (mapSize(2) - 1)] - (mapSize(2) - 1)/2) * xy(1);
    yrange = ([0 (mapSize(1) - 1)] - (mapSize(1) - 1)/2) * xy(2);
    set(himg, 'XData', xrange, 'YData', yrange);
    set(hsub, 'XLim', [xrange(1)-xy(1)/2 xrange(2)+xy(1)/2], ...
        'YLim', [yrange(1)-xy(2)/2 yrange(2)+xy(2)/2]);  
    daspect([xy 1]);
    title('apNumber')
    hold on
    hsoma = plot(xysoma(1), xysoma(2), 'wo');
    colorbar;
else
%     disp([currentMap.experiment ': No apNumber map found.']);
end

% mapAvg map
if isfield(currentMap, 'mapAvg')
    plotnum = plotnum + 1;
    if plotnum == plotToSkip; plotnum = plotnum + 1; end
    hsub = subplot(subRows, subCols, plotnum);
    himg = imagesc(currentMap.mapAvg);
    mapSize = size(currentMap.mapAvg);
    xrange = ([0 (mapSize(2) - 1)] - (mapSize(2) - 1)/2) * xy(1);
    yrange = ([0 (mapSize(1) - 1)] - (mapSize(1) - 1)/2) * xy(2);
    set(himg, 'XData', xrange, 'YData', yrange);
    set(hsub, 'XLim', [xrange(1)-xy(1)/2 xrange(2)+xy(1)/2], ...
        'YLim', [yrange(1)-xy(2)/2 yrange(2)+xy(2)/2]);  
    daspect([xy 1]);
    title('mapAvg')
    hold on
    hsoma = plot(xysoma(1), xysoma(2), 'wo');
    colorbar;
else
%     disp([currentMap.experiment ': No mapAvg map found.']);
end


% ============= subroutines ============================= 
function B = nan2inf(A, polarity)
if nargin == 1
    polarity = 1;
end
A(isnan(A))=inf * polarity;
B = A;